﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using System.Timers;

namespace SpriteEditor
{
    public class SpriteCalculation
    {
        public static int SELECT_RANGE_EXPAND = 2; // Variables for program setting
        public static int SELECT_RANGE_CORNER = 4; // Variables for program setting
       

        public static bool isInCirle(Point p, Point I , int R)
        {
            return ( (( p.X - I.X ) * ( p.X - I.X )) + ((p.Y - I.Y)*(p.Y - I.Y)) <= (R*R) ) ;
        }
        public static bool isInCirle(Point p, Point I)
        {
            return isInCirle(p, I, SELECT_RANGE_CORNER);
        }
        public static int wichCornerPointInRect(Point p, Rectangle rec)
        {
            Point topLeft = new Point();
            topLeft.X = rec.X;
            topLeft.Y = rec.Y;
            Point buttomLeft = new Point();
            buttomLeft.X = rec.X;
            buttomLeft.Y = rec.Y + rec.Height;
            Point buttomRight = new Point();
            buttomRight.X = rec.X + rec.Width;
            buttomRight.Y = rec.Y + rec.Height;
            Point topRight = new Point();
            topRight.X = rec.X + rec.Width;
            topRight.Y = rec.Y;

            if (isInCirle(buttomRight, p))
                return frmMain.BUTTOM_RIGHT;
            else if( isInCirle( topLeft , p))
                return frmMain.TOP_LEFT;
            else if (isInCirle( topRight , p))
                return frmMain.TOP_RIGHT;
            else if(isInCirle( buttomLeft, p))
                return frmMain.BUTTOM_LEFT;
            else return -1;

        }
        public static int Max(int aaa, int bbb)
        {
            return aaa > bbb ? aaa : bbb;
        }

        public static int Min(int aaa, int bbb)
        {
            return aaa < bbb ? aaa : bbb;
        }

        public static bool IsPointInRect(Point p_vt, Rectangle r_rect)
        {
            if ((p_vt.X >= r_rect.X && p_vt.X <= r_rect.X + r_rect.Width)
                && (p_vt.Y >= r_rect.Y && p_vt.Y <= r_rect.Y + r_rect.Height))
                return true;
            return false;
        }

        public static bool IsPointAtTopLineOfRect(Point p_vt, Rectangle r_rect)
        {
            if ((p_vt.X > r_rect.X && p_vt.X < r_rect.X + r_rect.Width)
                 && ((p_vt.Y <= r_rect.Y + SELECT_RANGE_EXPAND) && (p_vt.Y >= r_rect.Y))
                )
                return true;
            return false;
        }

        public static bool IsPointAtBottomLineOfRect(Point p_vt, Rectangle r_rect)
        {
            if ((p_vt.X > r_rect.X && p_vt.X < r_rect.X + r_rect.Width)
                && ((p_vt.Y <= r_rect.Y + r_rect.Height) && (p_vt.Y >= r_rect.Y + r_rect.Height - SELECT_RANGE_EXPAND))
                )
                return true;
            return false;
        }

        public static bool IsPointAtLeftLineOfRect(Point p_vt, Rectangle r_rect)
        {
            if (p_vt.Y >= r_rect.Y && p_vt.Y <= r_rect.Y + r_rect.Height
                && ((p_vt.X <= r_rect.X + SELECT_RANGE_EXPAND) && (p_vt.X >= r_rect.X))
                )
                return true;
            return false;
        }

        public static bool IsPointAtRightLineOfRect(Point p_vt, Rectangle r_rect)
        {
            if (p_vt.Y >= r_rect.Y && p_vt.Y <= r_rect.Y + r_rect.Height
                && ((p_vt.X <= r_rect.X + r_rect.Width) && (p_vt.X >= r_rect.X + r_rect.Width - SELECT_RANGE_EXPAND))
                )
                return true;
            return false;
        }

        public static bool IsPointAtMiddleOfRect(Point p_vt, Rectangle r_rect)
        {
            if ((p_vt.X > r_rect.X + SELECT_RANGE_EXPAND && p_vt.X < r_rect.X + r_rect.Width - SELECT_RANGE_EXPAND)
                && (p_vt.Y > r_rect.Y + SELECT_RANGE_EXPAND && p_vt.Y < r_rect.Y + r_rect.Height - SELECT_RANGE_EXPAND))
                return true;
            return false;
        }

        public static int ConvertToRootX(int cr_posX, Point root_pos)
        {
            return cr_posX + root_pos.X;
        }

        public static int ConvertToRootY(int cr_posY, Point root_pos)
        {
            return cr_posY + root_pos.Y;
        }

        public static Point ConvertToRoot(Point cr_pos, Point root_pos)
        {
            return new Point(cr_pos.X + root_pos.X, cr_pos.Y + root_pos.Y);
        }

        public static Rectangle ConvertToRoot(Rectangle cr_rect, Point root_pos)
        {
            return new Rectangle(cr_rect.X + root_pos.X, cr_rect.Y + root_pos.Y, cr_rect.Width, cr_rect.Height);
        }

        public static int DeConvertToRootX(int cr_posX, Point root_pos)
        {
            return cr_posX - root_pos.X;
        }

        public static int DeConvertToRootY(int cr_posY, Point root_pos)
        {
            return cr_posY - root_pos.Y;
        }

        public static Point DeConvertToRoot(Point cr_pos, Point root_pos)
        {
            return new Point(cr_pos.X - root_pos.X, cr_pos.Y - root_pos.Y);
        }

        public static Rectangle DeConvertToRoot(Rectangle cr_rect, Point root_pos)
        {
            return new Rectangle(cr_rect.X - root_pos.X, cr_rect.Y - root_pos.Y, cr_rect.Width, cr_rect.Height);
        }

        public static Image FlipImage(Image image, bool flipHorizontally, bool flipVertically)
        {
            if (!flipHorizontally && !flipVertically)
                return image;

            Bitmap flippedImage = new Bitmap(image.Width, image.Height);

            using (Graphics g = Graphics.FromImage(flippedImage))
            {
                //Matrix transformation
                Matrix m = null;
                if (flipVertically && flipHorizontally)
                {
                    m = new Matrix(-1, 0, 0, -1, 0, 0);
                    m.Translate(flippedImage.Width, flippedImage.Height, MatrixOrder.Append);
                }
                else if (flipVertically)
                {
                    m = new Matrix(1, 0, 0, -1, 0, 0);
                    m.Translate(0, flippedImage.Height, MatrixOrder.Append);
                }
                else if (flipHorizontally)
                {
                    m = new Matrix(-1, 0, 0, 1, 0, 0);
                    m.Translate(flippedImage.Width, 0, MatrixOrder.Append);
                }

                //Draw
                g.Transform = m;
                g.DrawImage(image, 0, 0);

                //clean up
                m.Dispose();
            }

            return (Image)flippedImage;
        }

        public static Bitmap FlipBitmap(Bitmap image, bool flipHorizontally, bool flipVertically)
        {
            if (!flipHorizontally && !flipVertically)
                return image;

            Bitmap flippedImage = new Bitmap(image);

            using (Graphics g = Graphics.FromImage(flippedImage))
            {
                //Matrix transformation
                Matrix m = null;
                if (flipVertically && flipHorizontally)
                {
                    m = new Matrix(-1, 0, 0, -1, 0, 0);
                    m.Translate(flippedImage.Width, flippedImage.Height, MatrixOrder.Append);
                }
                else if (flipVertically)
                {
                    m = new Matrix(1, 0, 0, -1, 0, 0);
                    m.Translate(0, flippedImage.Height, MatrixOrder.Append);
                }
                else if (flipHorizontally)
                {
                    m = new Matrix(-1, 0, 0, 1, 0, 0);
                    m.Translate(flippedImage.Width, 0, MatrixOrder.Append);
                }

                //Draw
                g.Transform = m;
                g.DrawImage(image, 0, 0);

                //clean up
                m.Dispose();
            }

            return flippedImage;
        }

        public static bool isRelativePath(String m_path)
        {
            if (m_path == null || m_path == "")
                return false;

            if (m_path[0] == '.')
                return true;

            if (m_path.LastIndexOf("\\") == -1)
                return true;

            return false;
        }
    }
}
